package com.cloud.pole.shop.controller.order;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.exception.ParamException;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.common.util.RegexUtils;
import com.cloud.pole.shop.controller.IndexController;
import com.cloud.pole.shop.model.vo.IndexVo;
import com.cloud.pole.shop.model.vo.goods.GoodsDetailVo;
import com.cloud.pole.shop.model.vo.order.ShoppingCartVo;
import com.cloud.pole.shop.service.IndexService;
import com.cloud.pole.shop.service.goods.GoodsService;
import com.cloud.pole.shop.service.order.ShoppingCartService;

import lombok.extern.slf4j.Slf4j;

/*
*2019年2月21日
*cloud-shop
*author:frank-chen
*/
@Controller
@Slf4j
@RequestMapping("/shoppingCart/front")
public class ShoppingCartController {

	@Autowired
	private ShoppingCartService shoppingCartService;
	
	@ResponseBody
	@RequestMapping("/getShoppingCartList")
    public ApiRestResponse<ShoppingCartVo> getShoppingCartList(
    		@RequestHeader(defaultValue = "") String token){   	
    	try {
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, shoppingCartService.getShoppingCartList(token));
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
	
	@ResponseBody
	@RequestMapping("/addShoppingCart")
    public ApiRestResponse<Integer> addShoppingCart(
    		@RequestHeader(defaultValue = "") String token,
    		@RequestParam(defaultValue = "") String specId, 
    		@RequestParam(defaultValue = "") String productNum, 
    		@RequestParam(defaultValue = "") String goodsId){   	
    	try {
			ParamException.err(StringUtils.isBlank(token), "token不能为空");    		
			ParamException.err(StringUtils.isBlank(specId), "specId不能为空");
			ParamException.err(StringUtils.isBlank(productNum), "productNum不能为空");
			ParamException.err(StringUtils.isBlank(goodsId), "goodsId不能为空");
			ParamException.err(!RegexUtils.isPositiveInteger(specId), "specId错误");
			ParamException.err(!RegexUtils.isPositiveInteger(productNum), "goodsId错误");
			ParamException.err(!RegexUtils.isPositiveInteger(goodsId), "goodsId错误");
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, 
					shoppingCartService.addShoppingCart(token, Integer.parseInt(specId), Integer.parseInt(productNum), Integer.parseInt(goodsId)));
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
	
	@ResponseBody
	@RequestMapping("/delShoppingCart")
    public ApiRestResponse<Boolean> delShoppingCart(
    		@RequestHeader(defaultValue = "") String token,
    		@RequestParam(defaultValue = "") String cartId){   	
    	try {
			ParamException.err(StringUtils.isBlank(token), "token不能为空");    		
			ParamException.err(StringUtils.isBlank(cartId), "cartId不能为空");
			ParamException.err(!RegexUtils.isPositiveInteger(cartId), "cartId错误");
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, 
					shoppingCartService.delShoppingCart(token, Integer.parseInt(cartId)));
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }

}
